home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / apps / 93 / applic / sdbiex.c < prev    next >
C/C++ Source or Header  |  1987-01-15  |  7KB  |  277 lines

  1. /* SDB - import/export command routines */
  2.  
  3. #include "sdbio.h"
  4.  
  5. extern int dbv_token;
  6. extern char dbv_tstring[];
  7. extern int dbv_tvalue;
  8.  
  9. /* db_import - import tuples from a file */
  10. int *db_import(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9)
  11.   char *fmt;
  12. {
  13.     struct scan *sptr;
  14.     struct attribute *aptr;
  15.     char fname[STRINGMAX+1],avalue[STRINGMAX+1];
  16.     int tcnt,astart,i,eofile;
  17.     FILE *fp;
  18.  
  19.     /* check for a command line */
  20.     if (fmt != NULL)
  21.         db_scan(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9);
  22.  
  23.     /* checks for "<filename> into <relation-name>" */
  24.     if (db_ntoken() == ID)
  25.         strcat(dbv_tstring,".dat");
  26.     else if (dbv_token != STRING)
  27.         return (db_nerror(SYNTAX));
  28.     strcpy(fname,dbv_tstring);
  29.     if (db_ntoken() != INTO)
  30.         return (db_nerror(SYNTAX));
  31.     if (db_ntoken() != ID)
  32.     return (db_nerror(SYNTAX));
  33.  
  34.     /* open the relation */
  35.     if ((sptr = db_ropen(dbv_tstring)) == NULL)
  36.     return (FALSE);
  37.  
  38.     /* open the input file */
  39.     if ((fp = fopen(fname,"r")) == NULL)
  40.         return (db_nerror(INPFNF));
  41.  
  42.     /* import tuples */
  43.     eofile = FALSE;
  44.     for (tcnt = 0; ; tcnt++) {
  45.  
  46.         /* get attribute values */
  47.         astart = 1;
  48.         for (i = 0; i < NATTRS; i++) {
  49.  
  50.             /* get a pointer to the current attribute */
  51.         aptr = &sptr->sc_relation->rl_header.hd_attrs[i];
  52.  
  53.         /* check for the last attribute */
  54.         if (aptr->at_name[0] == 0)
  55.             break;
  56.  
  57.             /* input the tuple */
  58.             if (fgets(avalue,STRINGMAX,fp) == 0) {
  59.             eofile = TRUE;
  60.             break;
  61.             }
  62.             avalue[strlen(avalue)-1] = EOS;
  63.  
  64.         /* store the attribute value */
  65.         db_aput(aptr,&sptr->sc_tuple[astart],avalue);
  66.  
  67.             /* update the attribute start */
  68.             astart += aptr->at_size;
  69.         }
  70.  
  71.         /* store the new tuple */
  72.         if (!eofile) {
  73.             if (!db_rstore(sptr)) {
  74.             db_rclose(sptr);
  75.             return (FALSE);
  76.             }
  77.         }
  78.         else
  79.             break;
  80.     }
  81.  
  82.     /* close the relation */
  83.     db_rclose(sptr);
  84.  
  85.     /* close the input file */
  86.     fclose(fp);
  87.  
  88.     /* check number of tuples imported */
  89.     if (tcnt != 0) {
  90.  
  91.     /* print tuple count */
  92.         printf("[ %d imported ]\n",tcnt);
  93.     }
  94.     else
  95.     printf("[ none imported ]\n");
  96.  
  97.     /* return successfully */
  98.     return (TRUE);
  99. }
  100.  
  101. /* db_export - export tuples to a file */
  102. int *db_export(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9)
  103.   char *fmt;
  104. {
  105.     struct scan *sptr;
  106.     struct attribute *aptr;
  107.     char rname[STRINGMAX+1],avalue[STRINGMAX+1];
  108.     int tcnt,astart,i;
  109.     FILE *fp;
  110.  
  111.     /* check for a command line */
  112.     if (fmt != NULL)
  113.         db_scan(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9);
  114.  
  115.     /* checks for "<relation-name> [ into <filename> ]" */
  116.     if (db_ntoken() != ID)
  117.     return (db_nerror(SYNTAX));
  118.     strcpy(rname,dbv_tstring);
  119.     if (!db_to(&fp,".dat"))
  120.         return (FALSE);
  121.  
  122.     /* open the relation */
  123.     if ((sptr = db_ropen(rname)) == NULL)
  124.     return (FALSE);
  125.  
  126.     /* export tuples */
  127.     for (tcnt = 0; db_rfetch(sptr); tcnt++) {
  128.  
  129.         /* get attribute values */
  130.         astart = 1;
  131.         for (i = 0; i < NATTRS; i++) {
  132.  
  133.             /* get a pointer to the current attribute */
  134.         aptr = &sptr->sc_relation->rl_header.hd_attrs[i];
  135.  
  136.         /* check for the last attribute */
  137.         if (aptr->at_name[0] == 0)
  138.         break;
  139.  
  140.         /* get the attribute value */
  141.         db_aget(aptr,&sptr->sc_tuple[astart],avalue);
  142.  
  143.             /* output the tuple */
  144.             fprintf(fp,"%s\n",avalue); 
  145.  
  146.             /* update the attribute start */
  147.             astart += aptr->at_size;
  148.         }
  149.     }
  150.  
  151.     /* close the relation */
  152.     db_rclose(sptr);
  153.  
  154.     /* close the output file */
  155.     if (fp != stdout)
  156.         fclose(fp);
  157.  
  158.     /* check number of tuples exported */
  159.     if (tcnt != 0) {
  160.  
  161.     /* print tuple count */
  162.         printf("[ %d exported ]\n",tcnt);
  163.     }
  164.     else
  165.     printf("[ none exported ]\n");
  166.  
  167.     /* return successfully */
  168.     return (TRUE);
  169. }
  170.  
  171. /* db_squeeze - squeeze deleted tuples from a relation file */
  172. int *db_squeeze(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9)
  173.   char *fmt;
  174. {
  175.     struct scan *sptr;
  176.  
  177.     /* check for a command line */
  178.     if (fmt != NULL)
  179.         db_scan(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9);
  180.  
  181.     /* checks for "<relation-name>" */
  182.     if (db_ntoken() != ID)
  183.     return (db_nerror(SYNTAX));
  184.  
  185.     /* open the relation */
  186.     if ((sptr = db_ropen(dbv_tstring)) == NULL)
  187.     return (FALSE);
  188.  
  189.     /* compress the relation file */
  190.     if (!db_rcompress(sptr)) {
  191.     db_rclose(sptr);
  192.     return (FALSE);
  193.     }
  194.  
  195.     /* close the relation */
  196.     db_rclose(sptr);
  197.  
  198.     /* return successfully */
  199.     return (TRUE);
  200. }
  201.  
  202. /* db_extract - extract a relation definition */
  203. int *db_extract(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9)
  204.   char *fmt;
  205. {
  206.     struct scan *sptr;
  207.     struct attribute *aptr;
  208.     char rname[STRINGMAX+1],aname[ANSIZE+1],*atype;
  209.     int i;
  210.     FILE *fp;
  211.  
  212.     /* check for a command line */
  213.     if (fmt != NULL)
  214.         db_scan(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9);
  215.  
  216.     /* checks for "<relation-name> [ into <filename> ]" */
  217.     if (db_ntoken() != ID)
  218.     return (db_nerror(SYNTAX));
  219.     strcpy(rname,dbv_tstring);
  220.     if (!db_to(&fp,".def"))
  221.         return (FALSE);
  222.  
  223.     /* open the relation */
  224.     if ((sptr = db_ropen(rname)) == NULL)
  225.     return (FALSE);
  226.  
  227.     /* output the relation definition */
  228.     fprintf(fp,"create %s (\n",rname);
  229.  
  230.     /* get attribute values */
  231.     for (i = 0; i < NATTRS; i++) {
  232.  
  233.         /* get a pointer to the current attribute */
  234.         aptr = &sptr->sc_relation->rl_header.hd_attrs[i];
  235.  
  236.     /* check for the last attribute */
  237.     if (aptr->at_name[0] == 0)
  238.         break;
  239.  
  240.     /* get the attribute name */
  241.     strncpy(aname,aptr->at_name,ANSIZE); aname[ANSIZE] = 0;
  242.  
  243.         /* determine the attribute type */
  244.         switch (aptr->at_type) {
  245.         case TCHAR:
  246.             atype = "char";
  247.             break;
  248.         case TNUM:
  249.             atype = "num";
  250.             break;
  251.         default:
  252.             atype = "<error>";
  253.             break;
  254.         }
  255.  
  256.         /* output the attribute definition */
  257.         if (strlen(aname) < 8)
  258.             fprintf(fp,"\t%s\t\t%s\t%d\n",aname,atype,aptr->at_size);
  259.         else
  260.             fprintf(fp,"\t%s\t%s\t%d\n",aname,atype,aptr->at_size);
  261.     }
  262.  
  263.     /* output the relation size */
  264.     fprintf(fp,") %d %d\n",sptr->sc_relation->rl_tmax,
  265.                         sptr->sc_relation->rl_text);
  266.  
  267.     /* close the relation */
  268.     db_rclose(sptr);
  269.  
  270.     /* close the output file */
  271.     if (fp != stdout)
  272.         fclose(fp);
  273.  
  274.     /* return successfully */
  275.     return (TRUE);
  276. }
  277.